import sqlalchemy

from sqlalchemy.orm import sessionmaker
from sqlalchemy import Column,String,create_engine,Table,DATE
from sqlalchemy import Column,DateTime,String,Integer,ForeignKey,func
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship,backref

base = declarative_base()


book_m2m_author = Table('book_m2m_author', base.metadata,
                        Column('book_id',Integer,ForeignKey('books.id')),
                        Column('author_id',Integer,ForeignKey('authors.id')),
                        )

class Book(base):
    __tablename__ = 'books'
    id = Column(Integer,primary_key=True)
    name = Column(String(64))
    pub_date = Column(DATE)
    # book表不知道第三张表，所以关联一下第三张表
    authors = relationship('Author',secondary=book_m2m_author,backref='books')
    def __repr__(self):
        return self.name







# class People(base):
#
#     __tablename__ = 'people'
#
#     id = Column(String(10),primary_key=True)
#     name = Column(String(10))
#
engine = create_engine('mysql+pymysql://root:22267721mq@localhost:3306/pythonclass')
# DB = sessionmaker(bind=engine)
# dbsessionmaker = DB()
#
# p1 = People(id='1',name='Andy')
# dbsessionmaker.add(p1)

base.metadata.create_all(engine)

# dbsessionmaker.commit()
# dbsessionmaker.close()